package com.iemes.controller.purchase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.PurchaseFromMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.ReceiveFormMap;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.entry.EntryMapper;
import com.iemes.mapper.material.PurchaseMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.mapper.material.RecevieMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/receive")
@Controller
public class ReceiveController extends BaseController {
	@Inject
	private PurchaseMapper purMapper;
	@Inject
	private QualityMapper qualityMapper;
	@Inject
	private RecevieMapper receiveMapper;
	
	@RequestMapping("examinereceive")
	public String examineprus(Model model){
		List<ReceiveFormMap> receiveFromMap=receiveMapper.findByWhere(new ReceiveFormMap());
		model.addAttribute("receive", receiveFromMap);
		List<PurchaseFromMap> purFormMaps=purMapper.findByWhere(new PurchaseFromMap());
		model.addAttribute("pur", purFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		List<DepartmentForMap> depFormMap=purMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mater_receive/mater_receive_list";
	}

	@ResponseBody
	@RequestMapping("recevie_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		
		ReceiveFormMap receFromMap = getFormMap(ReceiveFormMap.class);
		List<Map<String, Object>> receiveFormMaps=receiveMapper.getAllName(receFromMap);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		LuogicPaging(pageNow, pageSize, receiveFormMaps);
		pageView.setRecords(receiveFormMaps);
		return pageView;
		
		/*Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                map.put("startTime","2014-11-11 00:00:00" );
		map.put("endTime",sdf.format(new Date()) );
	        RegistMsgService  service = new RegistMsgService();
		System.out.println(service.getRegistMsgByCondition(map).size())*/
		
		
	}
	

	@RequestMapping("examinereceiveInfo")
	public String examinepru(Model model){
		List<ReceiveFormMap> receiveFromMap=receiveMapper.findByWhere(new ReceiveFormMap());
		model.addAttribute("receive", receiveFromMap);
		List<PurchaseFromMap> purFormMaps=purMapper.findByWhere(new PurchaseFromMap());
		model.addAttribute("pur", purFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		List<DepartmentForMap> depFormMap=purMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mater_receive/mater_receive_info";
	}

	//编辑订单
	@RequestMapping("receive_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		List<DepartmentForMap> depFormMap=receiveMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMap);
		List<BrandModelFormMap> brandModelFormMaps=receiveMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		if(Common.isNotEmpty(id))
		{
			
			ReceiveFormMap receiveFormMaps = receiveMapper.findbyFrist("id", id, ReceiveFormMap.class);
			List<Map<String, Object>> receiveFormMap = receiveMapper.getAllName(receiveFormMaps);
			for (int i = 0; i < receiveFormMap.size(); i++) {
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("materialNo", receiveFormMap.get(i).get("materialNo"));
				}
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("productName", receiveFormMap.get(i).get("productName"));
				}
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("productName", receiveFormMap.get(i).get("productName"));
				}
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("model", receiveFormMap.get(i).get("model"));
				}
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("unit", receiveFormMap.get(i).get("unit"));
				}
				if(receiveFormMaps.getStr("purchaseNo")==receiveFormMap.get(i).get("purchaseNo")){
					receiveFormMaps.put("unitPrice", receiveFormMap.get(i).get("unitPrice"));
				}
				
			}
			model.addAttribute("before", receiveFormMaps);
		}
		return Common.BACKGROUND_PATH + "/mater_receive/mater_receive_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="receive_editEntity")
	@SystemLog(module="物料管理",methods="领用表-修改表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		ReceiveFormMap receiveFormMap=getFormMap(ReceiveFormMap.class);
		baseMapper.editEntity(receiveFormMap);
		return "success";
	}
	
	@RequestMapping("recevie_addUI")
	public String addUI(Model model) throws Exception {
		List<DepartmentForMap> depFormMap=receiveMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMap);
		List<BrandModelFormMap> brandModelFormMaps=receiveMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		return Common.BACKGROUND_PATH + "/mater_receive/mater_receive_add";
	}
	
	@ResponseBody
	@RequestMapping("receive_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="领用表-新增物料进仓清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		ReceiveFormMap receiveFromMap = getFormMap(ReceiveFormMap.class);
		baseMapper.addEntity(receiveFromMap);
		// 物料清单子项
		return "success";
	}
}
